package personalPage;

import java.io.IOException;
import java.io.RandomAccessFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PhotoViewServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;

	
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		String nomeFile = System.getProperty("user.dir")+"/img/"+request.getParameter("id")+".jpg";
		String nomeFileUn = System.getProperty("user.dir")+"/img/unknown.jpg";
		RandomAccessFile f;
		try{
			f = new RandomAccessFile(nomeFile, "r");
		}catch (Exception e) {
			f = new RandomAccessFile(nomeFileUn, "r");
		}
		byte[] imageBytes = new byte[(int)f.length()];
		f.read(imageBytes);
		response.setContentType("image/jpeg");
		response.setContentLength(imageBytes.length);
		response.getOutputStream().write(imageBytes);
		response.getOutputStream().flush();
		response.getOutputStream().close(); 
		f.close();
		

	}
}
